package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.kaishengit.entity.Contact;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ContactDao {
	DBHelp<Contact> db=new DBHelp<Contact>();
	
	public boolean addcontact(Contact c){
		String sql="insert into t_contact (id,name,company,cellphone,phone,email,address,website,weibo,introduction,t_project_id,createtime) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		return db.executeSQL(sql, c.getId(),c.getName(),c.getCompany(),c.getCellphone(),c.getPhone(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getIntroduce(),c.getProid(),c.getCreatetime());
	}
	public List<Contact> findAll(){
		String sql="select * from t_contact";
		return db.executeQueryForList(sql, new ContactRowMapper());
		
	}
	
	public Contact findbyid(String id){
		String sql="select id,name,company,cellphone,phone,email,address,website,weibo,introduction,t_project_id,createtime from t_contact where id=?";
		return db.executeQueryForObject(sql, new ContactRowMapper(), id);
	}
	private class ContactRowMapper implements RowMapper<Contact>{
		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact contact=new Contact();
			contact.setId(rs.getString("id"));
			contact.setName(rs.getString("name"));
			contact.setCompany(rs.getString("company"));
			contact.setCellphone(rs.getString("cellphone"));
			contact.setPhone(rs.getString("phone"));
			contact.setEmail(rs.getString("email"));
			contact.setAddress(rs.getString("address"));
			contact.setWebsite(rs.getString("website"));
			contact.setWeibo(rs.getString("weibo"));
			contact.setIntroduce(rs.getString("introduction"));
			contact.setProid(rs.getString("t_project_id"));
			contact.setCreatetime(rs.getString("createtime"));
			return contact;
			
		}
		
	}
	public boolean del(String id) {
		String sql="delete from t_contact where id=?";
		return db.executeSQL(sql, id);
	}
	public boolean updatecontact(String id, String name, String company,
			String cellphone, String phone, String email, String address,
			String website, String weibo, String introducte) {
		String sql="update t_contact set name=?,company=?,cellphone=?,phone=?,email=?,address=?,website=?,weibo=?,introduction=? where id=?";
		return db.executeSQL(sql, name,company,cellphone,phone,email,address,website,weibo,introducte,id);
	}
}
